package com.demo.service.impl;

import com.demo.annotations.Priv;
import com.demo.annotations.Trans;
import com.demo.dao.UserDao;
import com.demo.entity.User;
import com.demo.enums.Role;
import com.demo.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author gy
 * @date 2022/6/15
 */
@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private final UserDao userDao = null;

    @Override
    public void addUser() {
        System.out.println("新增用户");

    }

    @Override
    public void testUseTime() {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 10000000; i++) {
            sb.append("a");
        }
    }

    @Priv({Role.USER, Role.ADMIN})
    @Override
    public void queryUser() {
        System.out.println("查询用户");
    }

    @Priv(Role.ADMIN)
    @Override
    public void delUser() {
        System.out.println("开始执行删除用户的业务处理...");
        System.out.println("删除用户");
    }


    @Priv({Role.USER, Role.ADMIN})
    @Override
    public void updateUser() {
        System.out.println("更新用户信息");
    }

    /**
     * 为了不再Service层使用conn造成耦合，开发TransactionManager专门用于管理事务
     * @param user
     */
    @Override
    @Trans
    public void addUser2(User user) {
        /* try {
            TransactionMnanager.startTran();//开启事务
            userDao.addUser(user);
            int i = 1 / 0;
            TransactionMnanager.commitTran();//提交事务
        }catch (Exception e){
            TransactionMnanager.rollbackTran();//回滚事务
            e.printStackTrace();
        }finally {
            TransactionMnanager.release();//释放资源
        }*/

        userDao.addUser2(user);
    }


    @Override
    @Trans
    public void delUser2(int id) {
        userDao.delUser2(id);
    }
}
